import request from '../utils/request'
import b1 from '../assets/b1.png'
import b2 from '../assets/b2.png'
import b3 from '../assets/b3.png'

// 获取课程列表
export function getCourseList(params) {
  return request({
    url: '/course/list',
    method: 'get',
    params: {
      page: params.page,
      pageSize: params.pageSize
    }
  }).then(response => {
    console.log('getCourseList API Success Response:', response)
    return response
  }).catch(error => {
    console.error('getCourseList API Error Response:', error)
    throw error
  })
}

// 获取课程详情
export function getCourseDetail(courseId) {
  return request({
    url: `/course/${courseId}`,
    method: 'get'
  })
}

// 获取课程章节
export function getCourseChapters(courseId) {
  return request({
    url: `/course/${courseId}/chapters`,
    method: 'get'
  })
}

// 获取课程评论
export function getCourseComments(courseId) {
  return request({
    url: `/course/${courseId}/comments`,
    method: 'get'
  })
}

// 获取章节资料
export function getChapterMaterials(chapterID) {
  return request({
    url: `/chapter/materials/${chapterID}`,
    method: 'get'
  })
}

// 搜索课程
export function searchCourses(params) {
  return request({
    url: '/course/search',
    method: 'get',
    params: {
      keyword: params.keyword,
      page: params.page,
      pageSize: params.pageSize
    }
  }).then(response => {
    console.log('searchCourses API Success Response:', response)
    return response
  }).catch(error => {
    console.error('searchCourses API Error Response:', error)
    throw error
  })
}

// 获取章节评论
export function getChapterComments(chapterID) {
  return request({
    url: `/chapter/${chapterID}/comments`,
    method: 'get'
  }).then(response => {
    console.log('getChapterComments API Success Response:', response)
    return response
  }).catch(error => {
    console.error('getChapterComments API Error Response:', error)
    throw error
  })
}

// 购买课程 - 调用后端产品支付接口
export function purchaseCourse(courseID) {
  return request({
    url: `/product/pay/${courseID}`,
    method: 'get'
  }).then(response => {
    console.log('purchaseCourse API Success Response:', response)
    return response
  }).catch(error => {
    console.error('purchaseCourse API Error Response:', error)
    // 如果是认证错误，给出更友好的提示
    if (error.message && error.message.includes('未登录')) {
      throw new Error('请先登录后再购买课程')
    }
    throw error
  })
}

// 获取我的课程列表 - 暂时注释，因为后端没有对应接口
export function getMyCourses(params = {}) {
  // 暂时返回模拟数据，因为后端没有对应接口
  const mockCourses = [
    { courseID: 1, name: 'Vue 3 入门与实战', img: b1, price: 99.00, progress: 30, instructor: '张老师' },
    { courseID: 2, name: 'React 全家桶详解', img: b2, price: 129.00, progress: 0, instructor: '李老师' },
    { courseID: 3, name: 'Node.js 从零到一', img: b3, price: 89.00, progress: 75, instructor: '王老师' },
  ];

  return Promise.resolve({
    code: 1,
    message: '获取成功',
    data: {
      items: mockCourses,
      total: mockCourses.length,
      page: params.page || 1,
      pageSize: params.pageSize || 10
    }
  });
  
  // 原始代码（后端接口不存在）
  /*
  return request({
    url: '/course/my-courses',
    method: 'get',
    params: {
      page: params.page || 1,
      pageSize: params.pageSize || 10
    }
  }).then(response => {
    console.log('getMyCourses API Success Response:', response)
    return response
  }).catch(error => {
    console.error('getMyCourses API Error Response:', error)
    throw error
  })
  */
}

// 检查课程购买状态 - 暂时注释，因为后端没有对应接口
export function checkCoursePurchaseStatus(courseID) {
  // 暂时返回未购买状态，因为后端没有对应接口
  return Promise.resolve({
    code: 1,
    message: '查询成功',
    data: {
      isPurchased: false
    }
  });
  
  // 原始代码（后端接口不存在）
  /*
  return request({
    url: `/course/purchase-status/${courseID}`,
    method: 'get'
  }).then(response => {
    console.log('checkCoursePurchaseStatus API Success Response:', response)
    return response
  }).catch(error => {
    console.error('checkCoursePurchaseStatus API Error Response:', error)
    throw error
  })
  */
} 